package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.itheima.common.TokenHolder;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.mapper.OrderDetailMapper;
import com.itheima.mapper.OrdersMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.Orders;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.OrdersService;
import com.itheima.vo.OrderAndDetailVO;
import com.itheima.vo.OrderListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 小帅蓝
 * @Date 2022/6/20 20:49
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private AddressBookMapper addressBookMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Override
    public Integer getCount(String number, String userId) {
        return ordersMapper.getCount(number, userId);
    }

    @Override
    public List<Orders> queryAll(String beginTime, String endTime, String number, Integer page, Integer pageSize) {
        Integer offset = getOffset(page, pageSize);
        return ordersMapper.queryAll(beginTime, endTime, number, offset, pageSize);
    }

    @Override
    public boolean updateStatus(Orders orders) {
        int result = ordersMapper.updateStatus(orders);
        return result > 0;
    }

    @Override
    public void insertOrder(Orders orders) {
        String userId = TokenHolder.getCurrentId();
        //先从购物车中获取菜品信息
        List<ShoppingCart> list = shoppingCartMapper.queryAll(userId);
        //获取订单总金额
        BigDecimal amount = list.stream().map(s -> s.getAmount().multiply(new BigDecimal(s.getNumber().toString()))).reduce(BigDecimal.ZERO, BigDecimal::add);
        //查询收货地址
        AddressBook address = addressBookMapper.queryById(orders.getAddressBookId());
        String orderId = new Snowflake().nextIdStr();
        orders.setId(orderId);
        orders.setNumber(orderId);
        orders.setUserId(userId);
        orders.setAmount(amount);
        orders.setPhone(address.getPhone());
        orders.setAddress(address.getDetail());
        orders.setConsignee(address.getConsignee());
        orders.setStatus(1);
        ordersMapper.insertOrder(orders);

        //添加订单详情
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        list.forEach(s -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtil.copyProperties(s, orderDetail);
            orderDetail.setId(new Snowflake().nextIdStr());
            orderDetail.setOrderId(orderId);

            orderDetails.add(orderDetail);
        });
        orderDetailMapper.insertOrderDetail(orderDetails);

        //清空购物车
        shoppingCartMapper.deleteAll(userId);
    }

    @Override
    public List<OrderListVO> getOrderAndDetailsList(Integer page, Integer pageSize) {
        Integer offset = getOffset(page, pageSize);
        return ordersMapper.getOrderList(TokenHolder.getCurrentId(), offset, pageSize);
    }

    /**
     * 从第几条开始，offset - pageSize
     *
     * @param page     当前页
     * @param pageSize 每页条数
     * @return 每页起始数
     */
    public Integer getOffset(Integer page, Integer pageSize) {
        return (page - 1) * pageSize;
    }
}
